#include "XSystem/XCore.h"
#include "GSelectChar.h"

// Initialize the window
GSelectChar::GSelectChar(XResourcesManager *data) : XWindow(data)
{
	// Default values
    x_positionX = 185;
    x_positionY = 150;
    x_width = 430;
    x_height = 300;
	x_view.Move(-x_positionX, -x_positionY);

	x_init = false;

	x_windowBackground.SetImage(x_data->GetImage("gui/selectchar.png"));

	// Create the widgets
	x_widgets["labelcharname1"] = new XLabel(data, 80, 40, "");
	x_widgets["button1"] = new XPushButton(data, 50, 16, 20, 40);

	x_widgets["labelcharname2"] = new XLabel(data, 80, 60, "");
	x_widgets["button2"] = new XPushButton(data, 50, 16, 20, 60);

	x_widgets["labelcharname3"] = new XLabel(data, 80, 80, "");
	x_widgets["button3"] = new XPushButton(data, 50, 16, 20, 80);
}

// Draws the window
void GSelectChar::Draw()
{
    if (x_visible)
    {
        XWindow::Draw();
    }
}

// Refresh the window
void GSelectChar::CheckState(const sf::Input &input)
{
	if (!x_init)
	{
		x_widgets["labelcharname1"]->SetText(x_data->GetString("charname1"), true);
		x_widgets["labelcharname2"]->SetText(x_data->GetString("charname2"), true);
		x_widgets["labelcharname3"]->SetText(x_data->GetString("charname3"), true);

		x_init = true;
	}

	// Check the state of the widgets
	XWindow::CheckState(input);

	if (x_widgets["button1"]->state() == XG_ST_LBUTTON_RELEASED)
		x_data->NewPacket("selectchar", "1", "");

	if (x_widgets["button2"]->state() == XG_ST_LBUTTON_RELEASED)
		x_data->NewPacket("selectchar", "2", "");

	if (x_widgets["button2"]->state() == XG_ST_LBUTTON_RELEASED)
		x_data->NewPacket("selectchar", "2", "");

}

// Check the queries of the widgets
void GSelectChar::CheckQuery()
{
	if (!x_data->queueEmpty("selectchar"))
	{
		functions function = x_data->nextQueue("selectchar");

		if (function.argument == "loadmap")
		{
			x_data->NewQueue("ui", "setscreen", "main");
		} 
	}
}
